import 'dart:convert' show json;
import 'package:loxen/mode/comment_list.dart';
import 'package:loxen/mode/address_list.dart';
import 'package:loxen/mode/comment_mode.dart';
import 'package:loxen/mode/inspiration/catgory_list.dart';
import 'package:loxen/mode/inspiration/inspiration_info_bean.dart';
import 'package:loxen/mode/inspiration/inspiration_list.dart';
import 'package:loxen/mode/inspiration/inspiration_page.dart';
import 'package:loxen/mode/like_mode.dart';
import 'package:loxen/mode/master/file_info_list.dart';
import 'package:loxen/mode/mine/like_list.dart';
import 'package:loxen/mode/mine/mine_comment_list.dart';
import 'package:loxen/mode/mine/mine_information_mode.dart';
import 'package:loxen/mode/mine/order_list_mode.dart';
import 'package:loxen/mode/mine/user_follower_list.dart';
import 'package:loxen/mode/mine/user_info_edit.dart';
import 'package:loxen/mode/mine/user_privacy.dart';
import 'package:loxen/mode/order_detail_mode.dart';
import 'package:loxen/mode/order_list_mode.dart';
import 'package:loxen/mode/staff_info_mode.dart';
import 'package:loxen/mode/study/study_info_bean.dart';

import 'operation_list_mode.dart';

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class BaseMode<T> {
  int code;
  T data;

  BaseMode._create({this.code, this.data});

  factory BaseMode(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    } else {
      return BaseMode._create(
          code: asT<int>(jsonRes['code']),
          data: createObj<T>(asT<Map<String, dynamic>>(jsonRes['data'])));
    }
  }

  static createObj<T>(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) return null;
    if (T.toString() == 'MineInformation') {
      return MineInformation.fromJson(asT<Map<String, dynamic>>(jsonRes['results'])) as T;
    } else if (T.toString() == 'CommentList') {
      return CommentList.fromJson(jsonRes);
    } else if (T.toString() == 'AddressList') {
      return AddressList.fromJson(jsonRes);
    } else if (T.toString() == 'CategoryList') {
      return CategoryList.fromJson(jsonRes);
    } else if (T.toString() == 'Like') {
      return Like.fromJson(jsonRes);
    } else if (T.toString() == 'Comment') {
      return Comment.fromJson(jsonRes);
    } else if (T.toString() == 'InspirationInfoList') {
      return InspirationInfoList.fromJson(jsonRes);
    } else if (T.toString() == 'InspirationPage') {
      return InspirationPage.fromJson(jsonRes);
    } else if (T.toString() == 'FollowerList') {
      return FollowerList.fromJson(jsonRes);
    } else if (T.toString() == 'LikeList') {
      return LikeList.fromJson(jsonRes);
    } else if (T.toString() == 'MineCommentList') {
      return MineCommentList.fromJson(jsonRes);
    } else if (T.toString() == 'UserPrivacy') {
      return UserPrivacy.fromJson(jsonRes);
    } else if (T.toString() == 'UserInfoEdit') {
      return UserInfoEdit.fromJson(asT<Map<String, dynamic>>(jsonRes['results'])) as T;
    } else if (T.toString() == 'InspirationInfo') {
      return InspirationInfo.fromJson(asT<Map<String, dynamic>>(jsonRes['results'])) as T;
    } else if (T.toString() == 'StudyInfo') {
      return StudyInfo.fromJson(asT<Map<String, dynamic>>(jsonRes['results'])) as T;
    } else if (T.toString() == 'FileInfoList') {
      return FileInfoList.fromJson(jsonRes);
    } else if (T.toString() == 'OperationList') {
      return OperationList.fromJson(jsonRes);
    } else if (T.toString() == 'OrderList') {
      return OrderList.fromJson(jsonRes);
    } else if (T.toString() == 'OrderDetailMode') {
      return OrderDetailMode.fromJson(asT<Map<String, dynamic>>(jsonRes['result'])) as T;
    }else if(T.toString() == 'StaffOrderList') {
      return StaffOrderList.fromJson(jsonRes);
    }else if(T.toString() == 'StaffInfo') {
      return StaffInfo.fromJson(asT<Map<String, dynamic>>(jsonRes['results']));
    }
    return null;
  }

  @override
  String toString() {
    return json.encode(this);
  }
}
